package stack;

public class ArrayStack {
    private int maxSize;//栈的容量
    private int[] stack;//用数组模拟栈
    private int top = -1;//栈顶
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //栈空
    public boolean isEmpty() {
        return top == -1;
    }
    //栈满
    public boolean isFull() {
        return top == maxSize-1;
    }
    //往栈中添加数据
    public void push(int num) {
        if(isFull()) {
            System.out.println("栈满无法添加数据~~~~");
            return;
        }
        top++;
        stack[top] = num;
    }
    //从栈中取出数据
    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈空，无法取出数据~~~~");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //遍历栈中数据
    public void list() {
        if(isEmpty()) {
            System.out.println("栈空，无法显示数据~~~~");
            return;
        }
        for (int i = top; i >=0 ; i--) {
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }
    public int peek() {
        return stack[top];
    }
}
